home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Linux / SLAX 6.0.8 / slax-6.0.8.iso / slax / base / 006-devel.lzm / usr / include / kopete / kopeteaccount.h < prev    next >
Encoding:
C/C++ Source or Header  |  2006-10-01  |  18.1 KB  |  555 lines

  1. /*
  2.     kopeteaccount.h - Kopete Account
  3.  
  4.     Copyright (c) 2003-2004 by Olivier Goffart       <ogoffart@ tiscalinet.be>
  5.     Copyright (c) 2003-2004 by Martijn Klingens      <klingens@kde.org>
  6.     Copyright (c) 2004      by Richard Smith         <kde@metafoo.co.uk>
  7.     Kopete    (c) 2002-2004 by the Kopete developers <kopete-devel@kde.org>
  8.  
  9.     *************************************************************************
  10.     *                                                                       *
  11.     * This library is free software; you can redistribute it and/or         *
  12.     * modify it under the terms of the GNU Lesser General Public            *
  13.     * License as published by the Free Software Foundation; either          *
  14.     * version 2 of the License, or (at your option) any later version.      *
  15.     *                                                                       *
  16.     *************************************************************************
  17. */
  18.  
  19. #ifndef KOPETEACCOUNT_H
  20. #define KOPETEACCOUNT_H
  21.  
  22. #include "kopeteonlinestatus.h"
  23.  
  24. #include "kopete_export.h"
  25.  
  26. #include <qobject.h>
  27. #include <qdict.h>
  28.  
  29. class QDomNode;
  30. class KActionMenu;
  31. class KConfigGroup;
  32.  
  33. namespace Kopete
  34. {
  35. class Contact;
  36. class Plugin;
  37. class Protocol;
  38. class MetaContact;
  39. class Group;
  40. class OnlineStatus;
  41. class BlackLister;
  42.  
  43. /**
  44.  * The Kopete::Account class handles one account.
  45.  * Each protocol should subclass this class in its own custom accounts class.
  46.  * There are few pure virtual method that the protocol must implement. Examples are:
  47.  * \li \ref connect()
  48.  * \li \ref disconnect()
  49.  * \li \ref createContact()
  50.  *
  51.  * If your account requires a password, derive from @ref PasswordedAccount instead of this class.
  52.  *
  53.  * The accountId is an @em constant unique id, which represents the login.
  54.  * The @ref myself() contact is one of the most important contacts, which represents
  55.  * the user tied to this account. You must create this contact in the contructor of your
  56.  * account and pass it to @ref setMyself().
  57.  *
  58.  * All account data is saved to @ref KConfig. This includes the accountId, the autoconnect flag and
  59.  * the color. You can save more data using @ref configGroup()
  60.  *
  61.  * When you create a new account, you have to register it with the account manager by calling
  62.  * @ref AccountManager::registerAccount.
  63.  *
  64.  * @author Olivier Goffart  <ogoffart@tiscalinet.be>
  65.  */
  66. class KOPETE_EXPORT Account : public QObject
  67. {
  68.     Q_OBJECT
  69.  
  70.     Q_ENUMS( AddMode )
  71.     Q_PROPERTY( QString accountId READ accountId )
  72.     Q_PROPERTY( bool excludeConnect READ excludeConnect WRITE setExcludeConnect )
  73.     Q_PROPERTY( QColor color READ color WRITE setColor )
  74.     Q_PROPERTY( QPixmap accountIcon READ accountIcon )
  75.     Q_PROPERTY( bool isConnected READ isConnected )
  76.     Q_PROPERTY( bool isAway READ isAway )
  77.     Q_PROPERTY( bool suppressStatusNotification READ suppressStatusNotification )
  78.     Q_PROPERTY( uint priority READ priority WRITE setPriority )
  79.  
  80. public:
  81.     /**
  82.      * \brief Describes how the account was disconnected
  83.      *
  84.      * Manual means that the disconnection was done by the user and no reconnection
  85.      * will take place. Any other value will reconnect the account on disconnection.
  86.      * The case where the password is wrong will be handled differently.
  87.      * @see @ref disconnected 
  88.      */
  89.     enum DisconnectReason {
  90.         OtherClient = -4,    ///< connection went down because another client connected the same account
  91.         BadPassword = -3,    ///< connection failed because password was incorrect
  92.         BadUserName = -2,    ///< connection failed because user name was invalid / unknown
  93.         InvalidHost = -1,    ///< connection failed because host is unreachable
  94.         Manual = 0,          ///< the user disconnected normally
  95.         ConnectionReset = 1, ///< the connection was lost
  96.         Unknown = 99         ///< the reason for disconnection is unknown
  97.     };
  98.  
  99.     /**
  100.      * @param parent the protocol for this account. The account is a child object of the
  101.      * protocol, so it will be automatically deleted when the protocol is.
  102.      * @param accountID the unique ID of this account.
  103.      * @param name the name of this QObject.
  104.      */
  105.     Account(Protocol *parent, const QString &accountID, const char *name=0L);
  106.     ~Account();
  107.  
  108.     /**
  109.      * \return the Protocol for this account
  110.      */
  111.     Protocol *protocol() const ;
  112.  
  113.     /**
  114.      * \return the unique ID of this account used as the login
  115.      */
  116.     QString accountId() const;
  117.  
  118.     /**
  119.      * \return The label of this account, for the GUI
  120.      */
  121.     QString accountLabel() const;
  122.  
  123.     /**
  124.      * \brief Get the priority of this account.
  125.      *
  126.      * Used for sorting and determining the preferred account to message a contact.
  127.      */
  128.     uint priority() const;
  129.  
  130.     /**
  131.      * \brief Set the priority of this account.
  132.      *
  133.      * @note This method is called by the UI, and should not be called elsewhere.
  134.      */
  135.     void setPriority( uint priority );
  136.  
  137.     /**
  138.      * \brief Set if the account should not log in automatically.
  139.      *
  140.      * This function can be used by the EditAccountPage. Kopete handles connection automatically.
  141.      * @sa @ref excludeConnect
  142.      */
  143.     void setExcludeConnect(bool);
  144.  
  145.     /**
  146.      * \brief Get if the account should not log in.
  147.      *
  148.       * @return @c true if the account should not be connected when connectAll at startup, @c false otherwise.
  149.      */
  150.     bool excludeConnect() const;
  151.  
  152.     /**
  153.      * \brief Get the color for this account.
  154.      *
  155.       * The color will be used to visually differentiate this account from other accounts on the
  156.      * same protocol.
  157.      *
  158.      * \return the user color for this account
  159.      */
  160.     const QColor color() const;
  161.  
  162.     /**
  163.      * \brief Set the color for this account.
  164.      *
  165.      * This is called by Kopete's account config page; you don't have to set the color yourself.
  166.      *
  167.      * @sa @ref color()
  168.      */
  169.     void setColor( const QColor &color);
  170.  
  171.     /**
  172.      * \brief Get the icon for this account.
  173.      *
  174.      * Generates an image of size @p size representing this account. The result is not cached.
  175.      *
  176.      * @param size the size of the icon. If the size is 0, the default size is used.
  177.      * @return the icon for this account, colored if needed
  178.      */
  179.     QPixmap accountIcon( const int size = 0 ) const;
  180.     
  181.     /**
  182.      * \brief change the account icon.
  183.      * by default the icon of an account is the protocol one, but it may be overide it.
  184.      * Set QString::null to go back to the default  (the protocol icon)
  185.      * 
  186.      * this call will emit colorChanged()
  187.      */
  188.     void setCustomIcon( const QString& );
  189.     
  190.     /**
  191.      * \brief return the icon base
  192.      * This is the custom account icon set with setIcon.   if this icon is null, then the protocol icon is used
  193.      * don't use this funciton to get the icon that need to be displayed, use accountIcon
  194.      */
  195.     QString customIcon() const;
  196.     
  197.             
  198.             
  199.  
  200.     /**
  201.      * \brief Retrieve the 'myself' contact.
  202.      *
  203.      * \return a pointer to the Contact object for this account
  204.      *
  205.      * \see setMyself().
  206.      */
  207.     Contact * myself() const;
  208.  
  209.     /**
  210.      * @brief Return the menu for this account
  211.      *
  212.      * You have to reimplement this method to return the custom action menu which will
  213.      * be shown in the statusbar. It is the caller's responsibility to ensure the menu is deleted.
  214.      *
  215.      * The default implementation provides a generic menu, with actions generated from the protocol's
  216.      * registered statuses, and an action to show the account's settings dialog.
  217.      *
  218.      * You should call the default implementation from your reimplementation, and add more actions
  219.      * you require to the resulting action menu.
  220.      *
  221.      * @see OnlineStatusManager::registerOnlineStatus
  222.      */
  223.     virtual KActionMenu* actionMenu() ;
  224.  
  225.     /**
  226.      * @brief Retrieve the list of contacts for this account
  227.      *
  228.      * The list is guaranteed to contain only contacts for this account,
  229.      * so you can safely use static_cast to your own derived contact class
  230.      * if needed.
  231.      */
  232.     const QDict<Contact>& contacts();
  233.  
  234.     /**
  235.      * Indicates whether or not we should suppress status notifications
  236.      * for contacts belonging to this account.
  237.      *
  238.      * This is used when we just connected or disconnected, and every contact has their initial
  239.      * status set.
  240.      *
  241.      * @return @c true if notifications should not be used, @c false otherwise
  242.      */
  243.     bool suppressStatusNotification() const;
  244.  
  245.     /**
  246.      * \brief Describes what should be done when the contact is added to a metacontact
  247.      * @sa @ref addContact()
  248.      */
  249.     enum AddMode {
  250.         ChangeKABC = 0,     ///< The KDE Address book may be updated
  251.         DontChangeKABC = 1, ///< The KDE Address book will not be changed
  252.         Temporary = 2       ///< The contact will not be added on the contactlist
  253.     };
  254.  
  255.     /**
  256.      * \brief Create a contact (creating a new metacontact if necessary)
  257.      *
  258.      * If a contact for this account with ID @p contactId is not already on the contact list,
  259.      * a new contact with that ID is created, and added to a new metacontact.
  260.      *
  261.      * If @p mode is @c ChangeKABC, MetaContact::updateKABC will be called on the resulting metacontact.
  262.      * If @p mode is @c Temporary, MetaContact::setTemporary will be called on the resulting metacontact,
  263.      * and the metacontact will not be added to @p group.
  264.      * If @p mode is @c DontChangeKABC, no additional action is carried out.
  265.      *
  266.      * @param contactId the @ref Contact::contactId of the contact to create
  267.      * @param displayName the displayname (alias) of the new metacontact. Leave as QString::null if
  268.      *                    no alias is known, then by default, the nick will be taken as alias and tracked if changed.
  269.      * @param group the group to add the created metacontact to, or 0 for the top-level group.
  270.      * @param mode the mode used to add the contact. Use DontChangeKABC when deserializing.
  271.      * @return the new created metacontact or 0L if the operation failed
  272.      */
  273.     MetaContact* addContact( const QString &contactId, const QString &displayName = QString::null, Group *group = 0, AddMode mode = DontChangeKABC ) ;
  274.  
  275.     /**
  276.      * @brief Create a new contact, adding it to an existing metacontact
  277.      *
  278.      * If a contact for this account with ID @p contactId is not already on the contact list,
  279.      * a new contact with that ID is created, and added to the metacontact @p parent.
  280.      *
  281.      * @param contactId the @ref Contact::contactId of the contact to create
  282.      * @param parent the parent metacontact (must not be 0)
  283.      * @param mode the mode used to add the contact. See addContact(const QString&,const QString&,Group*,AddMode) for details.
  284.      *
  285.      * @return @c true if creation of the contact succeeded or the contact was already in the list,
  286.      *         @c false otherwise.
  287.      */
  288.     bool addContact( const QString &contactId, MetaContact *parent, AddMode mode = DontChangeKABC );
  289.  
  290.     /**
  291.      * @brief Indicate whether the account is connected at all.
  292.      *
  293.      * This is a convenience method that calls @ref Contact::isOnline() on @ref myself().
  294.      * This function is safe to call if @ref setMyself() has not been called yet.
  295.      *
  296.      * @see @ref isConnectedChanged()
  297.      */
  298.     bool isConnected() const;
  299.  
  300.     /**
  301.      * @brief Indicate whether the account is away.
  302.      *
  303.      * This is a convenience method that queries @ref Contact::onlineStatus() on @ref myself().
  304.      * This function is safe to call if @ref setMyself() has not been called yet.
  305.      */
  306.     bool isAway() const;
  307.  
  308.     /**
  309.      * Return the @ref KConfigGroup used to write and read special properties
  310.      *
  311.      * "Protocol", "AccountId" , "Color", "AutoConnect", "Priority", "Enabled" , "Icon" are reserved keyword
  312.      * already in use in that group.
  313.      * 
  314.      * for compatibility, try to not use key that start with a uppercase
  315.      */
  316.     KConfigGroup *configGroup() const;
  317.  
  318.     /**
  319.      * @brief Remove the account from the server.
  320.      *
  321.      * Reimplement this if your protocol supports removing the accounts from the server.
  322.      * This function is called by @ref AccountManager::removeAccount typically when you remove the
  323.      * account on the account config page.
  324.      *
  325.      * You should add a confirmation message box before removing the account. The default
  326.      * implementation does nothing.
  327.      *
  328.      * @return @c false only if the user requested for the account to be deleted, and deleting the
  329.      * account failed. Returns @c true in all other cases.
  330.      */
  331.     virtual bool removeAccount();
  332.  
  333.     /**
  334.      * \return a pointer to the blacklist of the account
  335.      */
  336.     BlackLister* blackLister();
  337.  
  338.     /**
  339.      * \return @c true if the contact with ID @p contactId is in the blacklist, @c false otherwise.
  340.      */
  341.     virtual bool isBlocked( const QString &contactId );
  342.  
  343. protected:
  344.     /**
  345.      * \brief Set the 'myself' contact.
  346.      *
  347.      * This contact must be defined for every account, because it holds the online status
  348.      * of the account. You must call this function in the constructor of your account.
  349.      *
  350.      * The myself contact can't be deleted as long as the account still exists. The myself
  351.      * contact is used as a member of every ChatSession involving this account. myself's
  352.      * contactId should be the accountID. The online status of the myself contact represents
  353.      * the account's status.
  354.      * 
  355.      * The myself should have the @ref ContactList::myself() as parent metacontact
  356.      * 
  357.      */
  358.     void setMyself( Contact *myself );
  359.  
  360.     /**
  361.      * \brief Create a new contact in the specified metacontact
  362.      *
  363.      * You shouldn't ever call this method yourself. To add contacts, use @ref addContact().
  364.      *
  365.      * This method is called by @ref addContact(). In this method, you should create the
  366.      * new custom @ref Contact, using @p parentContact as the parent.
  367.      *
  368.      * If the metacontact is not temporary and the protocol supports it, you can add the
  369.      * contact to the server.
  370.      *
  371.      * @param contactId the ID of the contact to create
  372.      * @param parentContact the metacontact to add this contact to
  373.      * @return @c true if creating the contact succeeded, @c false on failure.
  374.      */
  375.     virtual bool createContact( const QString &contactId, MetaContact *parentContact ) =0;
  376.  
  377.  
  378.     /**
  379.      * \brief Sets the account label
  380.      *
  381.      * @param label The label to set
  382.      */
  383.     void setAccountLabel( const QString &label );
  384.  
  385. protected slots:
  386.     /**
  387.      * \brief The service has been disconnected
  388.      *
  389.      * You have to call this method when you are disconnected. Depending on the value of
  390.      * @p reason, this function may attempt to reconnect to the server.
  391.      *
  392.      * - BadPassword will ask again for the password
  393.      * - OtherClient will show a message box
  394.      *
  395.      * @param reason the reason for the disconnection.
  396.      */
  397.     virtual void disconnected( Kopete::Account::DisconnectReason reason );
  398.  
  399.     /**
  400.      * @brief Sets the online status of all contacts in this account to the same value
  401.      *
  402.      * Some protocols do not provide status-changed events for all contacts when an account
  403.      * becomes connected or disconnected. For such protocols, this function may be useful
  404.      * to set all contacts offline.
  405.      *
  406.      * Calls @ref Kopete::Contact::setOnlineStatus on all contacts of this account (except the
  407.      * @ref myself() contact), passing @p status as the status.
  408.      *
  409.      * @param status the status to set all contacts of this account except @ref myself() to.
  410.      */
  411.     void setAllContactsStatus( const Kopete::OnlineStatus &status );
  412.  
  413. signals:
  414.     /**
  415.      * The color of the account has been changed
  416.      * 
  417.      * also emited when the icon change
  418.      * @todo  probably rename to accountIconChanged
  419.      */
  420.     void colorChanged( const QColor & );
  421.  
  422.     /**
  423.      * Emitted when the account is deleted.
  424.      * @warning emitted in the Account destructor. It is not safe to call any functions on @p account.
  425.      */
  426.     void accountDestroyed( const Kopete::Account *account );
  427.  
  428.     /**
  429.      * Emitted whenever @ref isConnected() changes.
  430.      */
  431.     void isConnectedChanged();
  432.  
  433. private:
  434.     /**
  435.      * @internal
  436.      * Reads the configuration information of the account from KConfig.
  437.      */
  438.     void readConfig();
  439.  
  440. public:
  441.     /**
  442.      * @internal
  443.      * Register a new Contact with the account. This should be called @em only from the
  444.      * @ref Contact constructor, not from anywhere else (not even a derived class).
  445.      */
  446.     void registerContact( Contact *c );
  447.  
  448. public slots:
  449.     /**
  450.      * @brief Go online for this service.
  451.      *
  452.      * @param initialStatus is the status to connect with. If it is an invalid status for this
  453.      * account, the default online for the account should be used.
  454.      */
  455.     virtual void connect( const Kopete::OnlineStatus& initialStatus = OnlineStatus() ) = 0;
  456.  
  457.     /**
  458.      * @brief Go offline for this service.
  459.      *
  460.      * If the service is connecting, you should abort the connection.
  461.      *
  462.      * You should call the @ref disconnected function from this function.
  463.      */
  464.     virtual void disconnect( ) = 0 ;
  465.  
  466. public slots:
  467.     /**
  468.      * If @p away is @c true, set the account away with away message @p reason. Otherwise,
  469.      * set the account to not be away.
  470.      *
  471.      * @todo change ; make use of setOnlineStatus
  472.      */
  473.     virtual void setAway( bool away, const QString &reason = QString::null );
  474.  
  475.     /**
  476.      * Reimplement this function to set the online status
  477.      * @param status is the new status
  478.      * @param reason is the away message to set.
  479.      * @note If needed, you need to connect.  if the offline status is given, you should disconnect
  480.      */
  481.     virtual void setOnlineStatus( const Kopete::OnlineStatus& status , const QString &reason = QString::null ) = 0;
  482.  
  483.     /**
  484.      * Display the edit account widget for the account
  485.      */
  486.     void editAccount( QWidget* parent = 0L );
  487.  
  488.     /**
  489.      * Add a user to the blacklist. The default implementation calls
  490.      * blackList()->addContact( contactId )
  491.      *
  492.      * @param contactId the contact to be added to the blacklist
  493.      */
  494.     virtual void block( const QString &contactId );
  495.  
  496.     /**
  497.      * Remove a user from the blacklist. The default implementation calls
  498.      * blackList()->removeContact( contactId )
  499.      *
  500.      * @param contactId the contact to be removed from the blacklist
  501.      */
  502.     virtual void unblock( const QString &contactId );
  503.  
  504. private slots:
  505.     /**
  506.      * Restore online status and status message on reconnect.
  507.      */
  508.     virtual void reconnect(); 
  509.  
  510.     /**
  511.      * Track the deletion of a Contact and clean up
  512.      */
  513.     void contactDestroyed( Kopete::Contact * );
  514.  
  515.     /**
  516.      * The @ref myself() contact's online status changed.
  517.      */
  518.     void slotOnlineStatusChanged( Kopete::Contact *contact, const Kopete::OnlineStatus &newStatus, const Kopete::OnlineStatus &oldStatus );
  519.  
  520.     /**
  521.      * The @ref myself() contact's property changed.
  522.      */
  523.     void slotContactPropertyChanged( Kopete::Contact *, const QString &, const QVariant &, const QVariant & );
  524.  
  525.     /**
  526.      * Stop the suppression of status notification (connected to a timer)
  527.      */
  528.     void slotStopSuppression();
  529.  
  530. private:
  531.     class Private;
  532.     Private *d;
  533.  
  534. protected:
  535.     virtual void virtual_hook( uint id, void* data);
  536.  
  537. public:
  538.     /**
  539.      * @todo remove
  540.      * @deprecated  use configGroup
  541.      */
  542.     void setPluginData( Plugin* /*plugin*/, const QString &key, const QString &value ) KDE_DEPRECATED;
  543.  
  544.     /**
  545.      * @todo remove
  546.      * @deprecated  use configGroup
  547.      */
  548.     QString pluginData( Plugin* /*plugin*/, const QString &key ) const KDE_DEPRECATED;
  549. };
  550.  
  551. } //END namespace Kopete
  552.  
  553. #endif
  554.  
  555.